package com.jwdf.uda.ui.fragment.user.login;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.FragmentEnterCodeBinding;
import com.jwdf.uda.event.UserEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.ForgotPasswordStep1Action;
import com.jwdf.uda.http.action.ForgotPasswordStep2Action;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.ToastUtils;
import com.jwdf.uda.utils.UdaUtils;

import retrofit2.Response;

/**
 * Created by Administrator on 2016/5/31.
 */
public class EnterAuthCodeFragment extends BaseFragment {
    private FragmentEnterCodeBinding mDataBinding;
    private String mMobile;
    public View.OnClickListener mClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            UdaUtils.hideInputMethod(getContext(), mDataBinding.inputEdit);
            if(R.id.btn_verify_set == v.getId()) {
                mDataBinding.ivClearInput.setVisibility(View.GONE);
                String authCode = mDataBinding.inputEdit.getText().toString().trim();
                if(TextUtils.isEmpty(authCode)) {
                    ToastUtils.show(getContext(), getString(R.string.code_can_not_be_null));
                    return;
                }
                if(authCode.length() < 6) {
                    ToastUtils.show(getContext(), getString(R.string.please_correct_the_code));
                    return;
                }
                doVerifyCode(authCode);
            } else if(R.id.tv_send_code == v.getId()) {
                authMobieCode();
            } else if(R.id.iv_clear_input == v.getId()) {
                mDataBinding.inputEdit.setText("");
            }
        }
    };
    private TextWatcher mTextChangeListener = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //mDataBinding.inputEdit.setSelection(s.length());
        }

        @Override
        public void afterTextChanged(Editable s) {
            //            mDataBinding.btnVerifySet.setEnabled(s.length() >= 6 ? true : false);
            mDataBinding.ivClearInput.setVisibility(TextUtils.isEmpty(s) ? View.GONE : View.VISIBLE);
        }
    };
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mDataBinding = FragmentEnterCodeBinding.inflate(inflater, container, false);
        mDataBinding.setEnterAuthCodeFragment(this);
        initViews();
        return mDataBinding.getRoot();
    }
    private void initViews() {
        mMobile = getArguments().getString("mobile", "");
        mDataBinding.inputEdit.addTextChangedListener(mTextChangeListener);
        mDataBinding.ivClearInput.setOnClickListener(mClickListener);
        mDataBinding.tvSendMobile.setText(getString(R.string.auth_code_send, mMobile));
        mDataBinding.tvSendCode.setEnabled(false);
//        mDataBinding.btnVerifySet.setEnabled(false);
        ToastUtils.show(getContext(), R.string.auth_code_send_msg, Toast.LENGTH_LONG);
        startCountDown();
    }
    private void startCountDown() {
        CountDownTimer countDownTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                if (getView() != null) {
                    mDataBinding.tvSendCode.setText(getString(R.string.already_send_code, millisUntilFinished / 1000));
                }
            }

            @Override
            public void onFinish() {
                if (getView() != null) {
                    mDataBinding.tvSendCode.setText(getString(R.string.send_code_again));
                    mDataBinding.tvSendCode.setEnabled(true);
                }
            }
        };
        countDownTimer.start();
    }
    private void doVerifyCode(final String authCode) {
        showProgressDialog();
        onNewRequestCall(ForgotPasswordStep2Action.newInstance(getContext(), mMobile,authCode).request(new AHttpService.IResCallback<PbBean.PbResUserForgotPasswordStep2>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResUserForgotPasswordStep2> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if (getView()!=null) {
                    closeProgressDialog();
                    if (HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_net))) {
                        return;
                    }
                    UserEvent event = new UserEvent();
                    event.setEvent(UserEvent.EVENT_STEP_2);
                    event.setExtra(mMobile + "##" + authCode);
                    EventBusUtil.post(event);
                }
            }
        }));
    }
    private void authMobieCode() {
        onNewRequestCall(ForgotPasswordStep1Action.newInstance(getContext(), mMobile).request(new AHttpService.IResCallback<PbBean.PbResUserForgotPasswordStep1>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResUserForgotPasswordStep1> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if (getView()!=null) {
                    if (HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_net))) {
                        mDataBinding.tvSendCode.setText(getString(R.string.send_code_again));
                        mDataBinding.tvSendCode.setEnabled(true);
                        return;
                    }
                    mDataBinding.tvSendCode.setEnabled(false);
                    ToastUtils.show(getContext(), R.string.auth_code_send_msg, Toast.LENGTH_LONG);
                    startCountDown();
                }
            }
        }));
    }
}
